{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T10:35:19.843090Z",
     "start_time": "2020-02-29T10:35:19.136730Z"
    }
   },
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T10:35:19.850835Z",
     "start_time": "2020-02-29T10:35:19.843090Z"
    }
   },
   "outputs": [],
   "source": [
    "url_template = 'http://quotes.money.163.com/f10/gszl_{}.html'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T10:35:19.862817Z",
     "start_time": "2020-02-29T10:35:19.850835Z"
    }
   },
   "outputs": [],
   "source": [
    "stock_code = '600011'  #华能国际\n",
    "stock_company_url = url_template.format(stock_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T12:55:24.956353Z",
     "start_time": "2020-02-29T12:55:24.842234Z"
    }
   },
   "outputs": [],
   "source": [
    "response = requests.get(stock_company_url)\n",
    "response.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T13:34:08.533270Z",
     "start_time": "2020-02-29T13:34:08.454590Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<table class=\"table_bg001 border_box limit_sale table_details\">\n",
       " <tr>\n",
       " <td class=\"td_label\">组织形式</td>\n",
       " <td class=\"td_width160\">国有企业</td>\n",
       " <td class=\"td_label keep_line\">地域</td>\n",
       " <td class=\"td_width160\">北京</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">中文简称</td>\n",
       " <td class=\"td_width160\">华能国际</td>\n",
       " <td class=\"td_label keep_line\">办公地址</td>\n",
       " <td class=\"td_width160\">北京市西城区复兴门内大街6号华能大厦,香港湾仔港湾道25号海港中心11楼1105-07室</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">公司全称</td>\n",
       " <td class=\"td_width160\">华能国际电力股份有限公司</td>\n",
       " <td class=\"td_label keep_line\">公司电话</td>\n",
       " <td class=\"td_width160\">010-63226999</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">英文名称</td>\n",
       " <td class=\"td_width160\">Huaneng Power International,Inc.</td>\n",
       " <td class=\"td_label keep_line\">公司电子邮箱</td>\n",
       " <td class=\"td_width160\">zqb@hpi.com.cn</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">注册资本</td>\n",
       " <td class=\"td_width160\">1,569,809万元</td>\n",
       " <td class=\"td_label keep_line\" z=\"\">董事长</td>\n",
       " <td class=\"td_width160\">舒印彪</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">员工人数</td>\n",
       " <td class=\"td_width160\">57,220</td>\n",
       " <td class=\"td_label keep_line\">董事会秘书</td>\n",
       " <td class=\"td_width160\">黄朝全</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">法人代表</td>\n",
       " <td class=\"td_width160\">舒印彪</td>\n",
       " <td class=\"td_label keep_line\">董秘电话</td>\n",
       " <td class=\"td_width160\">010-63226999</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">总经理</td>\n",
       " <td class=\"td_width160\">赵克宇</td>\n",
       " <td class=\"td_label keep_line\">董秘传真</td>\n",
       " <td class=\"td_width160\">010-63226888</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">公司网址</td>\n",
       " <td class=\"td_width160\">www.hpi.com.cn</td>\n",
       " <td class=\"td_label keep_line\">董秘邮箱</td>\n",
       " <td class=\"td_width160\">cq_huang@hpi.com.cn</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">信息披露网址</td>\n",
       " <td class=\"td_width160\">www.sse.com.cn</td>\n",
       " <td class=\"td_label keep_line\">信息披露报纸名称</td>\n",
       " <td class=\"td_width160\">上海证券报,中国证券报</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">主营业务</td>\n",
       " <td colspan=\"3\">电力及热力销售、港口服务及运输服务等。</td> </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">经营范围</td>\n",
       " <td colspan=\"3\">投资建设、经营管理电厂及开发、投资、经营与电厂有关的以出口为主的其他相关企业;热力生产及销售;电力生产及销售。</td> </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">公司沿革</td>\n",
       " <td colspan=\"3\">    本公司是经原国家经济体制改革委员会批准,由华能国电与河北省建设投资公司、福建投资开发总公司、江苏省投资公司、辽宁能源总公司、大连市建设投资公司、南通市建设投资公司以及汕头市电力开发公司共同作为发起人,以发起设立方式于1994年6月30日在北京注册成立的股份有限公司。公司公开发行的A股于2001年12月6日在上海证券交易所上市。</td> </tr>\n",
       " </table>, <table class=\"table_bg001 border_box limit_sale table_details\">\n",
       " <tr>\n",
       " <td class=\"td_label\">成立日期</td>\n",
       " <td>1994-06-30</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">上市日期</td>\n",
       " <td>2001-12-06</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">发行方式</td>\n",
       " <td>--            </td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">面值</td>\n",
       " <td>--                元\n",
       "             </td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">发行数量</td>\n",
       " <td>--万股\n",
       "             </td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">发行价格</td>\n",
       " <td>--元\n",
       "             </td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">募资资金总额</td>\n",
       " <td>--万元\n",
       "             </td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">发行费用</td>\n",
       " <td>3582万元</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">发行中签率</td>\n",
       " <td>--%\n",
       "             </td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">发行市盈率</td>\n",
       " <td>--%\n",
       "             </td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">发行后每股收益</td>\n",
       " <td>--元</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">发行后每股净资产</td>\n",
       " <td>--元\n",
       "             </td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">上市首日开盘价</td>\n",
       " <td>13.30元</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">上市首日收盘价</td>\n",
       " <td>13.64元</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">上市首日换手率</td>\n",
       " <td>84.20%</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">主承销商</td>\n",
       " <td>国泰君安证券股份有限公司</td>\n",
       " </tr>\n",
       " <tr>\n",
       " <td class=\"td_label\">上市保荐人</td>\n",
       " <td>国泰君安证券股份有限公司</td>\n",
       " </tr>\n",
       " <tr class=\"dbrow\">\n",
       " <td class=\"td_label\">会计师事务所</td>\n",
       " <td>--</td>\n",
       " </tr>\n",
       " </table>]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soup=BeautifulSoup(response.text, features='lxml')\n",
    "tables = soup.select('table[class*=\"table_details\"]')\n",
    "tables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T13:39:33.924533Z",
     "start_time": "2020-02-29T13:39:33.918548Z"
    }
   },
   "outputs": [],
   "source": [
    "def parse_span(result, table):\n",
    "        for item in table.find_all('td', class_='td_label'):\n",
    "            for sibling in item.next_siblings:\n",
    "                if sibling.name == 'td':\n",
    "                    result[item.string] = str(sibling.string)\n",
    "                    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T13:39:34.356238Z",
     "start_time": "2020-02-29T13:39:34.349262Z"
    }
   },
   "outputs": [],
   "source": [
    "result={}\n",
    "for name_span in tables:\n",
    "    parse_span(result, name_span)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-29T13:39:35.429414Z",
     "start_time": "2020-02-29T13:39:35.424505Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'组织形式': '国有企业',\n",
       " '地域': '北京',\n",
       " '中文简称': '华能国际',\n",
       " '办公地址': '北京市西城区复兴门内大街6号华能大厦,香港湾仔港湾道25号海港中心11楼1105-07室',\n",
       " '公司全称': '华能国际电力股份有限公司',\n",
       " '公司电话': '010-63226999',\n",
       " '英文名称': 'Huaneng Power International,Inc.',\n",
       " '公司电子邮箱': 'zqb@hpi.com.cn',\n",
       " '注册资本': '1,569,809万元',\n",
       " '董事长': '舒印彪',\n",
       " '员工人数': '57,220',\n",
       " '董事会秘书': '黄朝全',\n",
       " '法人代表': '舒印彪',\n",
       " '董秘电话': '010-63226999',\n",
       " '总经理': '赵克宇',\n",
       " '董秘传真': '010-63226888',\n",
       " '公司网址': 'www.hpi.com.cn',\n",
       " '董秘邮箱': 'cq_huang@hpi.com.cn',\n",
       " '信息披露网址': 'www.sse.com.cn',\n",
       " '信息披露报纸名称': '上海证券报,中国证券报',\n",
       " '主营业务': '电力及热力销售、港口服务及运输服务等。',\n",
       " '经营范围': '投资建设、经营管理电厂及开发、投资、经营与电厂有关的以出口为主的其他相关企业;热力生产及销售;电力生产及销售。',\n",
       " '公司沿革': '    本公司是经原国家经济体制改革委员会批准,由华能国电与河北省建设投资公司、福建投资开发总公司、江苏省投资公司、辽宁能源总公司、大连市建设投资公司、南通市建设投资公司以及汕头市电力开发公司共同作为发起人,以发起设立方式于1994年6月30日在北京注册成立的股份有限公司。公司公开发行的A股于2001年12月6日在上海证券交易所上市。',\n",
       " '成立日期': '1994-06-30',\n",
       " '上市日期': '2001-12-06',\n",
       " '发行方式': '--            ',\n",
       " '面值': '--                元\\r\\n            ',\n",
       " '发行数量': '--万股\\r\\n            ',\n",
       " '发行价格': '--元\\r\\n            ',\n",
       " '募资资金总额': '--万元\\r\\n            ',\n",
       " '发行费用': '3582万元',\n",
       " '发行中签率': '--%\\r\\n            ',\n",
       " '发行市盈率': '--%\\r\\n            ',\n",
       " '发行后每股收益': '--元',\n",
       " '发行后每股净资产': '--元\\r\\n            ',\n",
       " '上市首日开盘价': '13.30元',\n",
       " '上市首日收盘价': '13.64元',\n",
       " '上市首日换手率': '84.20%',\n",
       " '主承销商': '国泰君安证券股份有限公司',\n",
       " '上市保荐人': '国泰君安证券股份有限公司',\n",
       " '会计师事务所': '--'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
